#!/usr/bin/python
# -*- coding: utf-8 -*-

import os, errno
import sys, getopt

def ReadFile ( file ):

    f = open(file)
    lines = f.readlines()
    f.close()
    tattiIcon = chr(240) + chr(159) + chr(146) + chr(169);
    for item in lines:
        item = item.strip()
        if ": " in item:
            timeDateUser = item.split(": ")

            if( ( timeDateUser[0].find(":") != -1) and ( timeDateUser[0].find(" - ") != -1) and ( timeDateUser[0].find(", ") != -1) ):
                 # make sure its the timeDateaUser string
                
                time = timeDateUser[0].split(", ")[0];
                date = timeDateUser[0].split(", ")[1].split(" - ")[0];
                user = timeDateUser[0].split(", ")[1].split(" - ")[1];
                tatti =  timeDateUser[1].count(tattiIcon);
                lowerCase = timeDateUser[1].lower();
                media = lowerCase.count("<media omitted>");
                tatti +=  lowerCase.count("tatti");
                tatti +=  lowerCase.count("shit");
                tatti +=  lowerCase.count("potti");
         

                user = user.replace(" ", "_");
                f = open(user + ".csv", 'a')
                f.write("%s, %s, %d, %d\n" %(date, time, media, tatti))
                f.close()
                

    for dirname, dirnames, filenames in os.walk("."):
        listUser = list();
        listSize = list();
        listMedia = list();
        listTatti = list();
        sortedSize = list();
        sortedMedia = list();
        sortedTatti = list();

        for csvFile in filenames:

            if "csv" in csvFile:
                f = open(csvFile)
                lines = f.readlines()
                count = len(lines);
                mediaCount = 0;
                tattiCount = 0;
                for line in lines:
                    mediaCount += int(line.split(",")[2]);
                    tattiCount += int(line.split(",")[3]);

                listUser.append(csvFile.split(".csv")[0]);
                listSize.append(count);
                listMedia.append(mediaCount);
                listTatti.append(tattiCount);
                f.close()

        sortedSize = listSize[:];
        sortedMedia = listMedia[:];
        sortedTatti = listTatti[:];
        sortedSize.sort();
        sortedMedia.sort();
        sortedTatti.sort();

        f2 = open("SortedByMessageCount" + ".csv", 'a');
        outSorted ( f2, sortedSize, listUser, listSize);
        f2.close();

        f2 = open("SortedByMediaCount" + ".csv", 'a');
        outSorted ( f2, sortedMedia, listUser, listMedia);
        f2.close();

        f2 = open("SortedByTattiCount" + ".csv", 'a');
        outSorted ( f2, sortedTatti, listUser, listTatti);
        f2.close();

def outSorted ( f2, sortedList, listUser, list):
    sl = sortedList[:];
    lu = listUser[:];
    l = list[:];
    
    for x in range (0, len(sl)):
        count = sl.pop();
        index = l.index(count);
        f2.write("%s, %s\n" %(lu.pop(index),l.pop(index)));



def main(argv):
    inputfile = ''
    for arg in argv:
        inputfile = arg

    os.system("rm -rf *.csv");
    ReadFile(inputfile)
    os.system("cat Summary.csv");
        
if __name__ == "__main__":
    main(sys.argv[1:])
